上篇文章介紹了要怎麼將 LightsPlugin 添加到 Spring 上,今天我們來嘗是將架構餵給 AI,讓他幫忙我們將架構生好,細部的設定在去微調到自己想要的就可以了
這次我們要生成的功能會是前幾篇有生成過的提示詞的功能,嘗試餵給 AI 後的結果
將前幾篇的提示詞跟昨天介紹的架構一起餵給 AI 讓他幫忙生成的結果如下
public class SmartHomeAssistant {
private boolean lightIsOn = false;
private boolean acIsOn = false;
private int acTemperature = 24; // Default temperature
@DefineKernelFunction(name = "change_light_state", description = "Changes the state of the room light")
public String changeLightState(
@KernelFunctionParameter(name = "action", description = "1 for turn on, 2 for turn off") int action) {
if (action == 1) {
lightIsOn = true;
return "A-1-1"; // Turn on light
} else if (action == 2) {
lightIsOn = false;
return "A-1-2"; // Turn off light
} else if (action == 3 && lightIsOn) {
return "A-1-3"; // Light is on
} else if (action == 4 && !lightIsOn) {
return "A-1-4"; // Light is off
}
return "Invalid action"; // Error case
}
@DefineKernelFunction(name = "change_ac_state", description = "Changes the state of the air conditioner")
public String changeAcState(
@KernelFunctionParameter(name = "action", description = "1 for turn on, 2 for turn off, 5 for increase temp, 6 for decrease temp, 7 for current temp") int action) {
if (action == 1) {
acIsOn = true;
return "A-2-1"; // Turn on AC
} else if (action == 2) {
acIsOn = false;
return "A-2-2"; // Turn off AC
} else if (action == 5) {
acTemperature++;
return "A-2-5"; // Increase temperature
} else if (action == 6) {
acTemperature--;
return "A-2-6"; // Decrease temperature
} else if (action == 7) {
return "A-2-7-" + acTemperature; // Current temperature
}
return "Invalid action"; // Error case
}
@DefineKernelFunction(name = "general_chat", description = "Handles general chat responses")
public String generalChatResponse(@KernelFunctionParameter(name = "message", description = "User input message") String message) {
return "T-1-Text: " + handleUnknownRequest(message); // Responds as a chat bot
}
private String handleUnknownRequest(String message) {
// Logic to handle unknown requests, returning a general response
return "I'm sorry, I can't help with that.";
}
}
基本上就跟我們想要的功能差不多,只要再將部分的內容做微調就完成了!
這次利用 AI 幫忙寫結構,省去了不少時間需要想結構的寫法,我們就可以更加專注於將這個功能完善,花更多時間去想還可以添加的功能。